<template>
  <div>
    <el-form :inline="true" :model="queryParam" style="margin-top: 20px; height: 33px">
      <el-form-item label="公司名称：" style="margin-left: 33px; font-weight: bold">
        <el-input v-model="queryParam.id" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-main>
      <el-scrollbar>
        <el-table :data="tableData" stripe style="width: 100%" max-height="300">
          <el-table-column fixed type="index" label="序号" width="100" align="center" />
          <el-table-column prop="companyName" label="公司名称" width="300" align="center" />
          <el-table-column prop="updateTime" label="审核时间" width="300" align="center">
            <template #default="scope">
              <div style="display: flex; align-items: center; margin-left: 30%">
                <span style="margin-left: 10px">{{
                  moment(scope.row.updateTime).format('YYYY-MM-DD  h:mm')
                }}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="碳排放报告" align="center">
            <template #default="scope">
              <el-button size="small" type="primary" @click="handlePrimary(scope.row)"
                >查看
              </el-button>
              <a :href="load"> <el-button size="small" type="success" @click="handleSuccess(scope.row)"
                >下载
              </el-button></a>
            </template>
          </el-table-column>
        </el-table>
      </el-scrollbar>
    </el-main>
  </div>
  <div>
    <el-pagination
      background
      layout="prev, pager, next"
      :page-count="totalPage"
      @change="doPageChange"
    />
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { CompanyControllerService, DataReviewControllerService } from '../../../../generated'
import moment from 'moment/moment'
import { useRouter } from 'vue-router'
import App from '@/App.vue'

const totalPage = ref(10)
const queryParam = ref({
  current: 0,
  pageSize: 10,
  id: ''
})
const router = useRouter()

interface Data {
  url: ''
  companyId: ''
  updateTime: ''
  companyName: ''
}

const load = ref()
const tableData = ref<Data>()
/**
 * 加载数据
 */
const loadData = async () => {
  const res = await DataReviewControllerService.listDataReviewVoByPageUsingPost(queryParam.value)
  tableData.value = res.data.records
  for (let tableDataKey in tableData.value) {
    const res2 = await CompanyControllerService.getByIdUsingPost(
      tableData.value[tableDataKey].companyId
    )
    tableData.value[tableDataKey].companyName = res2.data.companyName
  }
  // console.log(tableData.value)
  totalPage.value = Math.floor(
    (Number(queryParam.value.pageSize - 1) + Number(res.data.total)) /
      Number(queryParam.value.pageSize)
  )
}

onMounted(() => {
  loadData()
})
/**
 * 分页请求
 * @param page
 */
const doPageChange = (page: any) => {
  // queryParam.value.current = page
  loadData()
}

/**
 * 查询
 */
function onSubmit() {
  console.log(queryParam.value)
  loadData()
}

/**
 * 查看
 * @param index
 */
function handlePrimary(index: any) {
  console.log(index)

  // router.push({
  //   path: index.url
  // })
}

function handleSuccess(index: any) {
  console.log(index)
  load.value = index.url
}
</script>

<style scoped>
.pagination-style {
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
